/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controle;

import DAO.UsuarioDAO;
import Database.DBConnection;
import Model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Fellipe de Souza
 */
@WebServlet(name = "trataLogin", urlPatterns = {"/trataLogin"})
public class trataLogin extends Comando {

    Connection conexao;

    public void execute() throws ServletException, IOException, SQLException{
        getResponse().setContentType("text/html;charset=UTF-8");
        PrintWriter out = getResponse().getWriter();
        out.println();
        String login = getRequest().getParameter("Login");
        String senha = getRequest().getParameter("Senha");
        
        Usuario user = new UsuarioDAO().pesquisarLogin(login);
        if (user != null && user.getSenha().equals(senha)) {
            HttpSession session = getRequest().getSession(true);
            session.setAttribute("usuario", user);
            getResponse().sendRedirect("publicaNoticia.jsp");
        } else {
            out.println("<h1> Ocorreu um erro durante a autenticação");
            //response.sendRedirect("login.html");
        }
        out.close();
    }

}
